package at.fhj.swd10.eshop.web.view;

import at.fhj.swd10.eshop.domain.Product;
import at.fhj.swd10.eshop.web.application.ShopData;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 * @author David
 */
@ManagedBean
@ViewScoped
public class DeleteProductBacking implements Serializable {

    @ManagedProperty(value = "#{shopData}")
    private ShopData shopData;

    public String deleteProduct() throws Exception {
        Product product = shopData.getProductById(extractProductIdOutOfContext());
        shopData.deleteProduct(product);
        return "productOverview";
    }

    private int extractProductIdOutOfContext() throws Exception {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        return Integer.parseInt(context.getRequestParameterMap().get("index"));
    }

    public ShopData getShopData() {
        return shopData;
    }

    public void setShopData(ShopData shopData) {
        this.shopData = shopData;
    }
}
